package com.ag.lzw.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.ag.bean.TbIds;
import com.ag.common.AgException;
import com.ag.lzw.dao.LzwIdsDAO;
import com.ag.lzw.service.Lzw_IIDService;

@Service("lzw_IIDServiceImpl")
public class Lzw_IIDServiceImpl implements Lzw_IIDService {

	private static final Log log = LogFactory.getLog(Lzw_IIDServiceImpl.class);
	/**
	 * 主键维护类的DAO
	 */
	@Resource(name="lzwIdsDao")
	private LzwIdsDAO iDDao;

	/**
	 * 获取表主键的方法 传入参数：主键维护表行数，参照IDUtil类取静态字段值 返回：目标表主键当前值
	 */
	@Override
	public List<String> findTablePK(Long id, int n) throws AgException {
		log.debug("getting table primary key with id: " + id);
		List<String> idList = new ArrayList<String>();
		try {
			//TbIds ids = iDDao.findById(id);
			long tmpl=id;
			TbIds ids = iDDao.findById((int)tmpl);
			if(ids!=null){
				Long tmp = Long.valueOf(ids.getIdValues());
				for(int i=0;i<n;i++){
					idList.add(String.valueOf(tmp));
					tmp++;
				}
				ids.setIdValues(tmp.toString());
				iDDao.attachDirty(ids);
				return idList;
			}
			else{
				System.out.println("\n ***ids is null!");
				return null;
			}
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw new AgException(
					"get table primary key failed with id: " + id, re);
		}
	}

	/**
	 * 查找反馈的最新ID
	 */
	@Override
	public String findFeedBackPK(Long id) {
		long tmpl=id;
		TbIds ids = iDDao.findById((int)tmpl);
		if(ids!=null){
			Long tmp = Long.valueOf(ids.getIdValues());
			ids.setIdValues(String.valueOf(tmp+1));
			iDDao.attachDirty(ids);
			return String.valueOf(tmp);
		} else {
			return null;
		}
	}
	
	
}
